package com.haiqiu.tool.excel.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间转换工具
 * @author haiqiu
 */
public class HqDateUtils {
    public static final String DATE_TIME_PATTERN = "yyyy/MM/dd";
    public static final String DATE_TIME_EXCEL = "dd/MM/yy";

    public static final String YMD = "yyyy/mm/dd";
    public static final String YMDHM = "yyyy/m/d hh:mm";
    public static final String APHM= "上午/下午h时mm分";
    public static final String YMD1 = "yyyy年m月";
    public static final String YMD2 = "yyyy年m月d日";
    public static final String YMDHMS = "yyyy年mm月dd日 hh时mm分ss秒";

    public static Date parseDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_PATTERN);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date parseDate(String date, String type) {
        SimpleDateFormat format = new SimpleDateFormat(type);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String format(Date date, String format) {
        if (format == null || format.equals("")) {
            format = DATE_TIME_PATTERN;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }
}